package com.gupao.javavip.netty.push.client.processor.handler;

import com.gupao.javavip.netty.push.api.MsgType;
import com.gupao.javavip.netty.push.api.Request;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.apachecommons.CommonsLog;

@CommonsLog
public class PushMsgRequestClientHandler extends AbstractPushMsgRequestClientHandler {
    private MsgType msgType;

    public PushMsgRequestClientHandler(MsgType msgType) {
        this.msgType = msgType;
    }

    @Override
    protected GenericFutureListener<Future<? super Void>> buildSendListener(Request request) {
        return (writeFuture) -> {
            //消息发送成功
            if (writeFuture.isSuccess()) {
                log.info("消息[" + request.getMsg().getId() + "]提交成功！");
            }
            //消息发送失败
            else {
                log.error("消息[" + request.getMsg().getId() + "]提交失败...", writeFuture.cause());
            }
        };
    }

    @Override
    protected Request buildRequest() {
        if (msgType == MsgType.MSG) {
            return service.buildPushMsgRequest();
        }

        return service.buildPushNoticeRequest();
    }
}
